﻿using Dal;
using Entity;
using Entity.DTO;
using IDal;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Security.Principal;

namespace Bll
{
    /// <summary>
    /// 通知业务逻辑层
    /// </summary>
    public class NoticeBll : INoticeBll
    {
        INoticeDal _noticeDal;
        IUserInfoDal _userInfoDal;


        public NoticeBll(INoticeDal noticeDal, IUserInfoDal userInfoDal)
        {
            _noticeDal = noticeDal;
            _userInfoDal = userInfoDal;
        }


        /// <summary>
        /// 创建通知
        /// </summary>
        /// <returns></returns>
        public bool CreateNotice(string title, string content, string userId, out string msg)
        {
            Notice notice = _noticeDal.GetDbSet()
                .Where(d => d.Title == title).FirstOrDefault();

            if (notice != null)
            {
                msg = "通知已经存在";
                return false;
            }

            Notice entity = new Notice();
            //创建guid 赋值给通知id，目的是保证id唯一
            entity.Id = Guid.NewGuid().ToString();
            entity.Content = content;
            entity.Title = title;
            entity.CreateTime = DateTime.Now;
            entity.CreatorId = userId;

            //调用数据访问层创建通知
            bool isSuccess = _noticeDal.CreateEntity(entity);

            if (isSuccess)
            {
                msg = "成功";
            }
            else
            {
                msg = "失败";
            }
            return isSuccess;
        }


        /// <summary>
        /// 软删除通知
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteNotice(string id)
        {
            Notice notice = _noticeDal.GetDbSet().FirstOrDefault(u => u.Id == id);
            if (notice == null)
            {
                return false;
            }

            notice.IsDelete = true;

            return _noticeDal.UpdateEntity(notice);
        }

        /// <summary>
        /// 批量软删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool DeleteNotices(List<string> ids)
        {
            foreach (var id in ids)
            {
                Notice notice = _noticeDal.GetDbSet().FirstOrDefault(u => u.Id == id);
                if (notice == null)
                {
                    return false;
                }

                notice.IsDelete = true;

                _noticeDal.UpdateEntity(notice);
            }

            return true;
        }


        /// <summary>
        /// 根据通知id获取通知信息
        /// </summary>
        /// <param name="noticeId"></param>
        /// <returns></returns>
        public object GetNoticeById(string noticeId)
        {
            var data = (from n in _noticeDal.GetDbSet().Where(n => !n.IsDelete)  //通知
                        join u in _userInfoDal.GetDbSet().Where(u => !u.IsDelete)
                        on n.CreatorId equals u.Id into uTemp
                        from u2 in uTemp.DefaultIfEmpty()
                        select new
                        {
                            n.Id,
                            n.Title,
                            n.CreateTime,
                            n.Content,
                            Creator = u2.UserName
                        }).FirstOrDefault(n => n.Id == noticeId);

            if (data != null)
            {
                return new
                {
                    data.Content,
                    data.Id,
                    data.Title,
                    CreateTime = data.CreateTime.ToString("yyyy-MM-dd HH:mm:ss"),
                    data.Creator
                };
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 获取通知集合
        /// </summary>
        public object GetNoticeList(string title, int page, int limit, out int count)
        {

            //左连接
            var queryable = from n in _noticeDal.GetDbSet().Where(n => !n.IsDelete)  //通知
                            join u in _userInfoDal.GetDbSet().Where(u => !u.IsDelete)
                            on n.CreatorId equals u.Id into uTemp
                            from u2 in uTemp.DefaultIfEmpty()
                            select new
                            {
                                n.Id,
                                n.Title,
                                n.CreateTime,
                                n.Content,
                                Creator = u2.UserName
                            };


            //IQueryable<Notice> queryable = _noticeDal.GetNoticesDbSet().Where(u => !u.IsDelete).OrderByDescending(d => d.CreateTime);

            if (!string.IsNullOrEmpty(title))
            {
                queryable = queryable.Where(u => u.Title.Contains(title));
            }


            //查询总条数
            count = queryable.Count();

            var list = queryable.OrderByDescending(d => d.CreateTime).Skip((page - 1) * limit).Take(limit).ToList().Select(n => new
            {
                n.Id,
                n.Title,
                CreateTime = n.CreateTime.ToString("yyyy-MM-dd HH:mm:ss"),
                n.Content,
                n.Creator
            });

            return list;
        }



        /// <summary>
        /// 更新通知
        /// </summary>
        /// <returns></returns>
        public bool UpdateNotice(string noticeId, string title, string content, out string msg)
        {
            //根据账号找出通知的信息
            Notice entity = _noticeDal.GetDbSet().FirstOrDefault(u => u.Id == noticeId);
            if (entity == null)
            {
                msg = "未找到通知信息";
                return false;
            }

            //查询是否修改到其他的名字一样的情况
            Notice otherEntity = _noticeDal.GetDbSet().FirstOrDefault(u => u.Id != noticeId && u.Title == title);
            if (otherEntity != null)
            {
                msg = "此通知名字已存在";
                return false;
            }

            entity.Title = title;
            entity.Content = content;

            //调用数据访问层
            bool isSucess = _noticeDal.UpdateEntity(entity);
            msg = "成功";
            return isSucess;
        }


    }
}